# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.jsonUtils_v2.table2Json_Chen import Table2Json
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.pagingUtils import MyPaging

class CardDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertCard(self, info):
        """
        添加card 信息
        :param info:
        :return:
        """
        userId = info['userId']
        cardName = info['cardName']
        cardPhone = info['cardPhone']
        cardType = info['cardType']
        cardCode = info['cardCode']

        sql = "insert into zzh_card (user_id, card_name, card_phone, card_type, card_code) VALUES " \
              "(%s,%s,%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId, cardName, cardPhone, cardType, cardCode])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findCardById(self, cardId):
        """
        根据ID查询card信息
        :param cardId:
        :return:
        """
        sql = "select * from zzh_card WHERE card_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [cardId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.card_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findCardByConditon(self, **kwargs):
        """
        card表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        # 获取对应的映射字典
        fieldMap = fieldMap_Chen.cardFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)
        sortRule = SqlUtils.getSortRule(multiSort, fieldMap)

        if sortRule:
            sql = "select * from zzh_card WHERE "+ whereSentence + "ORDER BY "+sortRule
        else:
            sql = "select * from zzh_card WHERE " + whereSentence
        cur = self.conn.cursor()
        rs = cur.execute(sql)

        if rs:
            adinfo = cur.fetchall()
            lists = MyPaging().getPage(rs, pageIndex, pageSize, adinfo)
            adJson = Table2Json.card_list_to_json(lists)
            return adJson, rs
        else:
            return None, 0

    def deleteCard(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_card WHERE card_id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateCard(self, info):
        """
        更新card
        :param info:
        :return:
        """
        cardId = info['cardId']
        userId = info['userId']
        cardName = info['cardName']
        cardPhone = info['cardPhone']
        cardType = info['cardType']
        cardCode = info['cardCode']

        sql = "update zzh_card set user_id=%s, card_name=%s, card_phone=%s, card_type=%s, card_code=%s WHERE card_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [userId,cardName,cardPhone,cardType,cardCode, cardId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectCard(self,cardId):
        """
        根据ID查询，返回元组
        :param cardId:
        :return: data tuple
        """
        sql = "select * from zzh_card WHERE card_id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [cardId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr